package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.constant.ProductConstants;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.entity.ProductChange;
import com.bytz.modules.cms.product.model.ProductExcel;
import com.bytz.modules.cms.product.model.ProductModel;
import com.bytz.modules.cms.product.service.IAdminProductService;
import com.bytz.modules.cms.product.service.IProductChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "商品相关")
@RequestMapping("/product")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.Product.EDIT + "')")
public class ProductController extends BytzController<Product, IAdminProductService> {

    @Autowired
    private IAdminProductService productService;

    @Autowired
    private IProductChangeService productChangeService;

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<Product> queryById(@RequestParam(value = "id") String id) {

        return Result.ok(productService.getById(id));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<Product>> page(Product product,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {

        return Result.ok(productService.page(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initQueryWrapper(product, req.getParameterMap(), Product.class)
        ));
    }


    @ApiOperation("查询经销商产品列表")
    @RequestMapping(value = "/dealerPrice", method = RequestMethod.GET)
    public Result<IPage<ProductModel>> dealerPrice(Product product,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(name = "resellerId") String resellerId,
                                            HttpServletRequest req)  throws Exception {
        return Result.ok(productService.queryPage(resellerId, new Page<>(pageNo, pageSize), product, req.getParameterMap()));
    }

    @ApiOperation(value = "导出经销商产品列表", notes = "导出经销商产品列表")
    @RequestMapping(value = "/exportResellerProducts", method = RequestMethod.POST)
    public void exportResellerProducts(Product product,
                                       HttpServletRequest request,
                                       HttpServletResponse response,
                                       @RequestParam(name = "resellerId") String resellerId) throws Exception {

        productService.exportResellerProducts(resellerId, product, request.getParameterMap(), response);
    }

    @ApiOperation(value = "按列批量修改", notes = "按列批量修改")
    @RequestMapping(value = "/importChange", method = RequestMethod.POST)
    public Result<Boolean> importChange(HttpServletRequest request) {
        List<Product> productList = ExcelUtil.importGroup(request, Product.class, null);
        return Result.ok(productService.importChange(productList));
    }

    @ApiOperation(value = "商品信息变更导入模版", notes = "商品信息变更导入模版")
    @RequestMapping(value = "/change/template", method = RequestMethod.POST)
    public void changeTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("商品信息变更导入模版", "Sheet1", Product.class, response);
    }

    @ApiOperation(value = "导入商品", notes = "导入商品")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        List<ProductExcel> products = ExcelUtil.importGroup(request, ProductExcel.class, null);

        return Result.ok(productService.mergeProduct(products));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("批量添加商品", "Sheet1", ProductExcel.class, response);
    }

    @ApiOperation(value = "导出全部商品", notes = "导出全部商品")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public void export(HttpServletResponse response) throws Exception {

        productService.export(response);
    }

    @ApiOperation(value = "上架", notes = "上架")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result<Boolean> enable(@RequestParam String id) {
        return Result.ok(productService.changeStatus(id, ProductConstants.ProductStatus.ENABLE));
    }

    @ApiOperation(value = "下架", notes = "下架")
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public Result<Boolean> disable(@RequestParam String id) {
        return Result.ok(productService.changeStatus(id, ProductConstants.ProductStatus.DISABLE));
    }

    @ApiOperation(value = "商品变更历史记录", notes = "商品变更历史记录")
    @RequestMapping(value = "/changeList", method = RequestMethod.GET)
    public Result<List<ProductChange>> changeList(@RequestParam String id) {
        return Result.ok(productChangeService.getByProductId(id));
    }

    @ApiOperation("通过商品编码右模糊匹配")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<List<String>> productPackByCode(@RequestParam String number) {
        return Result.ok(productService.searchProductNumber(number));
    }

    @ApiOperation("根据商品编码获取一条")
    @RequestMapping(value = "/getByNumber", method = RequestMethod.GET)
    public Result<Product> getByNumber(@RequestParam String number) {
        return Result.ok(productService.getByNumber(number));
    }

    @ApiOperation(value = "编辑", notes = "编辑")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<Product> update(@RequestBody Product product) {
        return Result.ok(productService.saveOrUpdateSaveObject(product, true));
    }
}
